package aic.rest;

import aic.beans.Customer;
import aic.misc.DebugPrint;
import javax.jws.WebService;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.WebClient;

@WebService(portName = "RestWrapperPT", endpointInterface = "aic.rest.RestWrapperService", serviceName = "RestWrapperService")
public class RestWrapperServiceImpl implements RestWrapperService {

    public Customer retrieveCustomer(String id) {
        DebugPrint.println("REST-Wrapper :: Retrieve Customer id="+id);
        WebClient webClient = WebClient.create("http://localhost:8085/");
        return webClient.path("/CustomerManagementService/retrieveCustomer/"+id).accept("application/xml").get(Customer.class);
    }

    public String createCustomer(Customer customer) {
        DebugPrint.println("REST-Wrapper :: Create Customer " + customer);

        WebClient webClient = WebClient.create("http://localhost:8085/");
        Response response = webClient.path("/CustomerManagementService/createCustomer/").type("application/xml").accept("application/xml").post(customer);
        if(response.getStatus() == Response.Status.OK.getStatusCode())
            return "Customer createte with REST: OK";
        else if(response.getStatus() == Response.Status.FORBIDDEN.getStatusCode())
            return "Customer createte with REST: FORBIDDEN (Customer was null)";
        else if(response.getStatus() == Response.Status.CONFLICT.getStatusCode())
            return "Customer createte with REST: CONFLICT (Exception while adding customer to customerList)";

        return "REST-Wrapper :: Create Customer :: FAIL";
    }

    public String updateCustomer(Customer customer) {
        DebugPrint.println("REST-Wrapper :: Update Customer " + customer);
        WebClient webClient3 = WebClient.create("http://localhost:8085/");
        Response response = webClient3.path("/CustomerManagementService/updateCustomer/").type("application/xml").accept("application/xml").put(customer);
        if(response.getStatus() == Response.Status.OK.getStatusCode())
            return "Customer update with REST: OK";
        else if(response.getStatus() == Response.Status.FORBIDDEN.getStatusCode())
            return "Customer update with REST: FORBIDDEN (Customer was null)";
        else if(response.getStatus() == Response.Status.CONFLICT.getStatusCode())
            return "Customer update with REST: CONFLICT (Exception while adding customer to customerList)";

        return "REST-Wrapper :: Update Customer :: FAIL";
    }

    public String deleteCustomer(String id) {
        DebugPrint.println("Checking RESTful Service :: deleteCustomer id=Customer4");
        WebClient webClient4 = WebClient.create("http://localhost:8085/");
        Response response = webClient4.path("/CustomerManagementService/deleteCustomer/Customer4").accept("application/xml").delete();
        if(response.getStatus() == Response.Status.OK.getStatusCode())
            return "Customer delted with REST: OK";
        else if(response.getStatus() == Response.Status.FORBIDDEN.getStatusCode())
            return "Customer delted with REST: FORBIDDEN (Customer was null)";

        return "REST-Wrapper :: Delete Customer :: FAIL";
    }

    
}
